package com.nightsoul.ehcache.loaderwriter;

import java.util.Map;
import java.util.Map.Entry;

import org.ehcache.spi.loaderwriter.BulkCacheLoadingException;
import org.ehcache.spi.loaderwriter.BulkCacheWritingException;
import org.ehcache.spi.loaderwriter.CacheLoaderWriter;

import com.google.common.collect.Maps;

public class MyCacheLoaderWriter implements CacheLoaderWriter<String, Object> {
	

	@Override
	public Object load(String key) throws Exception {
		return "name".equals(key) ? "张三" : "姓名";
	}

	@Override
	public Map<String, Object> loadAll(Iterable<? extends String> keys)	throws BulkCacheLoadingException, Exception {
		Map<String, Object> map = Maps.newHashMap();
		for(String key : keys) {
			String value = "name".equals(key) ? "张三" : "姓名";
			map.put(key, value);
		}
		return map;
	}

	@Override
	public void write(String key, Object value) throws Exception {
		System.out.println("write sor: key=" + key + ",value=" + value);
	}

	@Override
	public void writeAll(Iterable<? extends Entry<? extends String, ? extends Object>> entries)
			throws BulkCacheWritingException, Exception {
		for(Map.Entry<? extends String, ? extends Object> entry : entries) {
			this.write(entry.getKey(), entry.getValue());
		}
	}

	@Override
	public void delete(String key) throws Exception {
		System.out.println("delete sor: key=" + key);
	}

	@Override
	public void deleteAll(Iterable<? extends String> keys)	throws BulkCacheWritingException, Exception {
		for(String key : keys) {
			this.delete(key);
		}
	}

}
